package com.warmheart.core.combine.oper;

import java.util.function.Supplier;

import javax.validation.constraints.NotNull;

import com.warmheart.core.co.builder.CoBuilder;
import com.warmheart.core.co.oper.Co;
import com.warmheart.core.combine.model.CombineModel;
import com.warmheart.core.cors.builder.CoRsBuilder;

/**
 * 
 * @ClassName: Join
 * @Description: 该类用作对指定联合查询条件进行处理
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 * @param <T>
 * @param <E>
 */
public class Join<T extends CoRsBuilder<E, ?>, E extends CoBuilder<T>> {

    private final CoRsBuilder<?, ?> mainT;

    private final T joinT;

    public Join(CoRsBuilder<?, ?> mainT, T joinT) {
        this.mainT = mainT;
        this.joinT = joinT;
    }

    public <P extends CoRsBuilder<PE, ?>, PE extends CoBuilder<P>, J extends CoRsBuilder<JE, ?>, JE extends CoBuilder<J>> On<T, E, P, PE, J, JE> on(
            @NotNull Supplier<Co<P, PE>> preSupplier, @NotNull Supplier<Co<J, JE>> joinSupplier) {
        return new On<T, E, P, PE, J, JE>(this, preSupplier.get(), joinSupplier.get());
    }

    public <P extends CoRsBuilder<PE, ?>, PE extends CoBuilder<P>> SingleOn<T, E, P, PE> on(
            @NotNull Supplier<Co<P, PE>> preSupplier) {
        return new SingleOn<T, E, P, PE>(this, preSupplier.get());
    }

    public CombineModel fetchCombineModel() {
        return this.fetchMain().getCombineModel();
    }

    public CoRsBuilder<?, ?> fetchMain() {
        return this.mainT;
    }

    public T fetchCore() {
        return this.joinT;
    }

    public String fetchJoinAlias() {
        return this.fetchCombineModel().getJoinAliasMap().get(this.joinT);
    }
}
